package com.gudaomai.wuye.web;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.gudaomai.wuye.entity.RetEntity;
import com.gudaomai.wuye.service.IMemoServ;
import com.gudaomai.wuye.common.CommonConstant;
import com.gudaomai.wuye.common.CommonException;
import com.gudaomai.wuye.common.MsgConstant;
import com.gudaomai.wuye.entity.House;
import com.gudaomai.wuye.entity.Memo;
import com.gudaomai.wuye.entity.MemoInput;
import com.gudaomai.wuye.web.BaseController;

import io.swagger.annotations.ApiOperation;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zenghualu
 * @since 2017-08-18
 */
@Controller
@RequestMapping("/wuye/memo")
public class MemoCtrl extends BaseController {
	
	@Autowired
	IMemoServ memoService;
	
	/** 
	 * add a new Memo
	 * 
	 * 1. Check if all required field are filled in.
	 * 2. Check if all the inputs are valid.
	 * 3. Check if the memo with the same properties does not exist in the DB.
	 * 4. Save the inputed values.
	 * 
	 *@param  
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("The memo settings page, click the add button, add memo")
	@RequestMapping(value = "/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public RetEntity addMemo(@RequestBody MemoInput memoInput) throws Exception {
		
		
		 RetEntity entity = new RetEntity();

			if(memoService.isExist(memoInput.getIdHouse())){
				// The memo already exists in the database
				
				throw new CommonException(CommonConstant.RESULT_CODE_CREATED, 
						getMessage(MsgConstant.RET_MEMO_ADD_FAILURE));
			} 
			else{
				// Create new memo entity
				Memo memoEntity = new Memo();
				// copy all user inputs to the new memo entity
				BeanUtils.copyProperties(memoInput, memoEntity);
				
				// Insert the house entity into the database
				memoService.insert(memoEntity);
				
				//Return message and HTTP result code
				entity.setRetCode(CommonConstant.RESULT_CODE_OK);
				entity.setRetMessage(getMessage(MsgConstant.RET_MEMO_ADD_SUCCESS));
			}	
			
			return entity;
	}
	
	/** 
	 * query Memo information
	 * 1. Check if the corresponding Id exists in the DB.
	 * 2. Read and return the result set from DB .
	 * 
	 *@param  id
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("The memo settings page, click the query button, query memo info")	
	@GetMapping("/id/{id}")
	public Memo getMemobyId(@PathVariable("id") String id) throws Exception{
		
       Memo memo = memoService.selectById(id);
		
       memo.setRetCode(CommonConstant.RESULT_CODE_OK);
       memo.setRetMessage(getMessage(MsgConstant.RET_MEMO_SELECT_SUCCESS));
		return memo;
	}
	
	/** 
	 * update the existing Memo information.
	 * 
	 * 1. Check if all required field are filled in.
	 * 2. Check if all the inputs are valid.
	 * 3. update the Memo information in the DB.
	 * 
	 *@param 
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("The memo settings page, click the modify button, update memo")
	@RequestMapping(value = "/", method = RequestMethod.POST)
	public RetEntity updateMemo(@RequestBody MemoInput memoInput) throws Exception{
				
		
		    RetEntity entity = new RetEntity();
			
			// update the memo entity in the database
			// Create new memo entity
			Memo memoEntity = new Memo();
			// copy all user inputs to the new memo entity
			BeanUtils.copyProperties(memoInput, memoEntity);
			
			memoService.updateById(memoEntity);
			
			//Return message and HTTP result code
			entity.setRetCode(CommonConstant.RESULT_CODE_OK);
			entity.setRetMessage(getMessage(MsgConstant.RET_MEMO_UPDATE_SUCCESS));
			
			return entity;
			
	}
	
	
	/** 
	 * delete the existing Memo
	 * 1. Check if the corresponding Id exists in the DB.
	 * 2. Delete the Memo information from the DB.
	 * 
	 *@param  id
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("The Memo settings page, click the delete button, delete memo")
	@RequestMapping(value="/id/{id}",method=RequestMethod.DELETE)
	public RetEntity delMemoByid(@PathVariable("id") String id) throws Exception{
		
        RetEntity entity = new RetEntity();
		
		memoService.deleteById(id);
		
		entity.setRetCode(CommonConstant.RESULT_CODE_OK);
		entity.setRetMessage(getMessage(MsgConstant.RET_MEMO_DELETE_SUCCESS));
		return entity;
	}
}
